{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "09cd8704",
   "metadata": {},
   "source": [
    "# Numpy (Numerical Python)\n",
    "\n",
    "## 导入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5f5935b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8723c82",
   "metadata": {},
   "source": [
    "## N维数组ndarray对象(N-dimensional array)\n",
    "\n",
    "N维数组`ndarray`简称为数组`array`，数组的特点：\n",
    "\n",
    "- 所有元素数据类型必须相同，不能像list对象一样数据混装\n",
    "- 数组大小不可变\n",
    "- 数组形状必须是矩形，类似于excel表格"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f38113ea",
   "metadata": {},
   "source": [
    "## 数组基本操作\n",
    "\n",
    "### 初始化一维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "e226f10d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'array([1, 2, 3, 4, 5, 6])'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1, 2, 3, 4, 5, 6])\n",
    "print(a)\n",
    "print(type(a))\n",
    "repr(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73fe8e02",
   "metadata": {},
   "source": [
    "### 和list对象的共性\n",
    "\n",
    "array对象有很多操作和list对象类似，array也是可变对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "58668c1e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "[10  2  3 40  5  6]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'array([10,  2,  3, 40,  5,  6])'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过索引获取元素值\n",
    "print(a[0])\n",
    "\n",
    "# 修改元素的值\n",
    "a[0] = 10\n",
    "repr(a)\n",
    "\n",
    "# 切片 array切片不会获得新数组，\n",
    "# 而是获得原数组的视图view，\n",
    "# 修改视图，原数组也会被修改\n",
    "b = a[3:]\n",
    "repr(b)\n",
    "print(a)\n",
    "b[0] = 40\n",
    "repr(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78a1a5d4",
   "metadata": {},
   "source": [
    "###  初始化二维数组或更高维度的数组\n",
    "\n",
    "传入通过嵌套列表进行N-维度数组的初始化。\n",
    "\n",
    "- 零维数组又称作标量scalar\n",
    "- 一维数组又称为向量vector\n",
    "- 二维数据又称为矩阵matrix\n",
    "- N维数组（大于二维）又称为张量tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "84472c2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8],\n",
       "       [ 9, 10, 11, 12]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 初始化二维数组\n",
    "a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32d91bdb",
   "metadata": {},
   "source": [
    "二维数组也可以看作是平面直角坐标，行看作x-axis，列看作是y-axis，可以使用嵌套列表的语法获取某个元素值，也可以使用坐标获取元素值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7e2c8f78",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "# 使用坐标获取元素值\n",
    "print(a[1, 3])\n",
    "\n",
    "# 嵌套列表语法获取元素值\n",
    "print(a[1][3])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "817008ce",
   "metadata": {},
   "source": [
    "## 数组属性\n",
    "\n",
    "- ndim 维度\n",
    "- shape 形状，返回行列元组(row, col)\n",
    "- size 大小，元素总数row*col\n",
    "- dtype 每个元素的数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "971bd80a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "(3, 4)\n",
      "12\n",
      "int64\n"
     ]
    }
   ],
   "source": [
    "# 维度\n",
    "print(a.ndim)\n",
    "\n",
    "# 形状\n",
    "print(a.shape)\n",
    "\n",
    "# 大小\n",
    "print(a.size)\n",
    "\n",
    "# 数据类型 array所有元素数据类型相同\n",
    "print(a.dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de93e321",
   "metadata": {},
   "source": [
    "## 创建基本数组\n",
    "\n",
    "- zeros() 元素全为0的数组\n",
    "- ones() 元素全为1的数组\n",
    "- emtpy() 指定形状的空数组，里面的元素值随机，这些值没有意义，用于初始化数组效率很高\n",
    "- arange() 连续元素数组，类似与range()函数\n",
    "- linspace() 定距等分数组\n",
    "\n",
    "数组初始化元素默认数据类型是float64，因此初始化得到的所有元素都有小数点，通过dtype参数可以指定"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "0817df61",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0. 0. 0.]\n",
      "[1. 1.]\n",
      "[1. 1.]\n",
      "[0 1 2 3]\n",
      "[2 4 6 8]\n",
      "[ 0.   2.5  5.   7.5 10. ]\n",
      "[1 1]\n"
     ]
    }
   ],
   "source": [
    "# 全是0的一位数组\n",
    "print(np.zeros(3))\n",
    "\n",
    "# 全是1的一维数组\n",
    "print(np.ones(2))\n",
    "\n",
    "# 空数组\n",
    "print(np.empty(2))\n",
    "\n",
    "# 一定范围的连续元素数组\n",
    "print(np.arange(4))\n",
    "print(np.arange(2, 9, 2))\n",
    "\n",
    "# 定距等分\n",
    "print(np.linspace(0, 10, num=5))\n",
    "\n",
    "# 指定数据类型\n",
    "x = np.ones(2, dtype=np.int64)\n",
    "print(x)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
